var express = require('express');
var router = express.Router();
var { shopsmodel,dzmodel,addressmodel,shopCommentModel,atypemodel,btypemodel,ctypemodel,feedbackmodel,goshopModel,announcermodel,activitymodel } = require('../../model/model')
var multiparty = require('multiparty')
//获取某人的活动
router.get('/getact', async (req, res) => {
  let {aid}=req.query
  
  let data=await activitymodel.find({aid:aid})
  res.send({
    code: 200,
    data
  })
})
//获取活动详情
router.get('/getactivitydeail', async (req, res) => {
  let {id}=req.query
  let data=await activitymodel.find({_id:id}).populate('aid')
  res.send({
    code: 200,
    data
  })
})
//获取活动
router.get('/getactivity', async(req, res) => {
  let data=await activitymodel.find()
  res.send({
    code: 200,
    data
  })
})
//添加发布者
router.post('/announcermodel', (req, res) => {
  announcermodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//添加活动
router.post('/addactivity', (req, res) => {
  activitymodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//全选
router.put("/all", async (req, res) => {
 let {status} =req.body
  console.log(status);
  await goshopModel.updateMany({},{staus:!status})
  res.send({
    code: 200, 
    msg:"全选成功"
  })
})

//删除购物车
router.delete("/dropShop", async (req, res) => {
  let { _id } = req.body
  console.log(_id);
  await goshopModel.deleteOne({ _id: _id })
  res.send({
    code: 200, 
    msg:"购物车内容删除成功"
  })
})


//修改购物车
router.put('/xgshopcar', async (req, res) => {
  let { status, id } = req.body
  console.log( status, id);
 await goshopModel.updateOne({_id:id},{staus:status})
  res.send({
    code: 200,
  })
})
//获取购物车
router.get('/getshopcar', async(req, res) => {
  let data=await goshopModel.find()
  res.send({
    code: 200,
    data
  })
})
//添加购物车
router.post('/addshopcar', (req, res) => {
  goshopModel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//添加投诉
router.post('/addfeedback', (req, res) => {
  feedbackmodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//添加一级
router.post('/addatype', (req, res) => {
  atypemodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//添加二级
router.post('/addbtype', (req, res) => {
  btypemodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})

//添加三级
router.post('/addctype', (req, res) => {
  ctypemodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//获取类型
router.get('/getatype', async(req, res) => {
  let data = await atypemodel.find()
  res.send({
    code: 200,
    data
  })
})
router.get('/getbtype', async(req, res) => {
  let data = await btypemodel.find()
  res.send({
    code: 200,
    data
  })
})
router.get('/getctype', async(req, res) => {
  let data = await ctypemodel.find()
  res.send({
    code: 200,
    data
  })
})


//删除数据
router.delete('/deladdress', async (req, res) => {
  await addressmodel.deleteOne({_id:req.body.id})
  res.send({
    code: 200
  })
})
//修改收货地址
router.put('/xgaddress', async (req, res) => {
  await addressmodel.updateOne({ _id: req.body.id }, req.body)
  res.send({
    code: 200,
    msg:'修改成功'
  })
})
//获取收货地址
router.get('/getaddress', async(req, res) => {
  let data=await addressmodel.find()
  res.send({
    code: 200,
    data
  })
})
//获取收货地址
router.get('/getaddr', async(req, res) => {
  let data=await addressmodel.find({default:true})
  res.send({
    code: 200,
    data
  })
})
// 修改默认值
router.put('/updefault', async (req, res) => {
  console.log(12);
  let { id } = req.body
  await addressmodel.updateOne({default:true},{default:false})
  await addressmodel.updateOne({_id:id},{default:true})
  res.send({
    code: 200,
  })
})
//添加收货地址
router.post('/addaddress', (req, res) => {
  addressmodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//上传图片
router.post('/upload', (req, res) => {
  let form = new multiparty.Form()
  form.uploadDir ='upload'
  form.parse(req, (err, formdata, filedata) => {
    if (err) {
      console.log(err);
    }
    console.log(filedata,'jkh');
    let fileurl='http://127.0.0.1:3000/'+filedata.file[0].path
    res.send({
      code:200,
      fileurl
    })
  })
})
//添加地址栏数据
router.post('/adddz', async (req, res) => {
  dzmodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//获取地址栏数据
router.get('/dzlist', async (req, res) => {
      let data = await dzmodel.find().lean();
      let obj = {};
      data.forEach(item => {
          obj[item._id] = item;
      });
      let result = [];
      data.forEach(item => {
          if (!item.aid) {
              result.push(item);
          } else {
              if (!obj[item.aid].children) {
                  obj[item.aid].children = [];
              }
              obj[item.aid].children.push(item);
          }
      });
      res.send({
          code: 200,
          msg: "查询成功",
          data: result
      });
});
  //添加商品
router.post('/addshop', (req, res) => {
  shopsmodel.create(req.body)
  res.send({
    code: 200,
    msg:'添加成功'
  })
})
//获取商品
router.get('/getshop', async (req, res) => {
  let arr = [{}]
  let obj={}
  let { sales, sort, id } = req.query
  if (id) arr.push({ _id: id })
  if (sales) obj = { sales: -1 }
  if(sort=='asc') obj={ sort: 1 }
  if (sort == 'desc') obj = { sort: -1 }
  console.log(obj);
  let data = await shopsmodel.find({$and:arr}).sort(obj)
  res.send({
    code: 200,
    data
  })
})

module.exports = router;